import { ContentTypeEnum, RequestBodyEnum, RequestContentTypeEnum, RequestDataTypeEnum, RequestParamsTypeEnum, type RequestParamsObjType } from "@/const";
import type { RequestConfigType, RequestGlobalConfigType } from "@/stores/types";
import { showMsg, translateStr } from "@/utils";
import { isString, method } from "lodash";
import { request } from "../request";

export const customApi = (targetParams:RequestConfigType,globalParams:RequestGlobalConfigType)=>{
    if(!targetParams || !globalParams) return
    // 优先使用组件的配置
    const {
        requestOriginUrl,
        requestParams:globalRequestParams
    } = globalParams
    const {
        // 请求地址
        requestUrl,
        // 普通 / sql
        requestContentType,
        // 获取数据的方式
        requestDataType,
        // 请求方式 get/post/del/put/patch
        requestHttpType,
        // 请求体类型 none / form-data / x-www-form-urlencoded / json /xml
        requestParamsBodyType,
        // SQL 请求对象
        // requestSQLContent,
        // 请求内容 params / cookie / header / body: 同 requestParamsBodyType
        requestParams: targetRequestParams
    } = targetParams

    if(requestDataType == RequestDataTypeEnum.STATIC) return
    if(!requestUrl) return

    let headers:any = {
        ...globalRequestParams[RequestParamsTypeEnum.HEADER],
        ...targetRequestParams[RequestParamsTypeEnum.HEADER]
    }
    headers = translateStr(headers)
    // data参数
    let data:RequestParamsObjType | FormData | string = {}
    // params参数
    let params:RequestParamsObjType = {...targetRequestParams[RequestParamsTypeEnum.PARAMS]}
    params = translateStr(params)
    // form处理
    let formData:FormData = new FormData()

    switch(requestParamsBodyType){
        case RequestBodyEnum.NONE:
            break;
        case RequestBodyEnum.JSON:
            headers['Content-Type'] = ContentTypeEnum.JSON
            data = translateStr(targetRequestParams[RequestParamsTypeEnum.BODY]['json'])
            if(isString(data)){
                data = JSON.parse(data)
            }
            break;
        case RequestBodyEnum.XML:
            headers['Content-Type'] = ContentTypeEnum.XML
            data = translateStr(targetRequestParams[RequestParamsTypeEnum.BODY]['xml'])
            break;
        case RequestBodyEnum.X_WWW_FORM_URLENCODED:
            headers['Content-Type'] = ContentTypeEnum.FORM_URLENCODED
            const bodyFormData = targetRequestParams[RequestParamsTypeEnum.BODY]['x-www-form-urlencoded']
            for (const key in bodyFormData) {
                formData.set(key,translateStr(bodyFormData[key]))
            }
            data = bodyFormData
            break;
        case RequestBodyEnum.FORM_DATA:
            headers['Content-Type'] = ContentTypeEnum.FORM_DATA
            const bodyFormUrlencoded = targetRequestParams[RequestParamsTypeEnum.BODY]['form-data']
            for (const key in bodyFormUrlencoded) {
                formData.set(key,translateStr(bodyFormUrlencoded[key]))
            }
            data = formData
            break;
        default:
            break;
    }

    // sql处理
    if(requestContentType == RequestContentTypeEnum.SQL){}

    try {
        const url = new Function("return `" + `${requestOriginUrl}${requestUrl}`.trim() + "`")()
        console.log(
            url,
            requestHttpType,
            data,
            params,
            headers);
        
        // 请求的参数处理需要根据后端处理相关。当前项目前端封装数据都是存放在请求体body中。
        return request({
            url,
            method:requestHttpType,
            data,
            params,
        })
    } catch (error) {
        showMsg("URL地址错误",'error')
        console.log(error);
    }
}